package struct.stack.realize;

import java.util.EmptyStackException;

/**
 * @author: 柒月
 * @date: 2025/5/2 下午7:48
 * @version: 1.0
 */
public class ListStack {

    private Node head;
    private int size;

    public ListStack() {
        head = null;
        size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public void push(int value) {
        Node newNode = new Node(value);
        if (isEmpty()){
            head = newNode;
        }else {
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    public int pop() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
        int value = head.data;
        head = head.next;
        size--;
        return value;
    }

    public int peek() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
        return head.data;
    }

    private static class Node {
        private final int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }
    }
}
